package com.svms.movement;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.Page;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.metainfo.ComponentInfo;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zul.Button;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;

import com.svms.service.vo.MovementVO;
import com.svms.utils.SvmsMessageUtils;
import com.svms.web.constants.SvmsResources;
import com.svms.web.factory.SvmsWebFactory;

public class ManageMovementCtrl extends GenericForwardComposer<Window> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private Window window_manage_movement;

	private Button btn_add_movement;
	private Button btn_search_movement_list;
	private Button btn_movement_refresh;
	private Button btn_movement_help;
	private Textbox txb_page_size;

	private Listbox lstbx_movements;
	private ListModelList<MovementVO> lst_model_movement;

	public Listbox getLstbx_movements() {
		return lstbx_movements;
	}

	public void setLstbx_movements(Listbox lstbx_movements) {
		this.lstbx_movements = lstbx_movements;
	}

	@Override
	public ComponentInfo doBeforeCompose(Page page, Component parent,
			ComponentInfo compInfo) {

		System.out.println("Movement doBeforeCompose ");
		loadMovementToListModal();

		return super.doBeforeCompose(page, parent, compInfo);
	}

	@Override
	public void doAfterCompose(Window comp) throws Exception {
		System.out.println("Movement doAfterCompose ");
		super.doAfterCompose(comp);
	}

	private void loadMovementToListModal() {
		List<MovementVO> mvo = SvmsWebFactory.movementService()
				.getMovementList();
		System.out.println("[][][]");
		System.out.println(mvo);
		System.out.println("[][][]");
		lst_model_movement = new ListModelList<MovementVO>(SvmsWebFactory
				.movementService().getMovementList());
	}

	/**
	 * Populates the Movement details list
	 * 
	 * @param event
	 * @throws Exception
	 */
	public void onCreate$window_manage_movement(Event event) throws Exception {
		System.out.println("Movement onCreate$window_manage_movement ");
		setMovementListModalToListBox();
		setPageSize();

	}

	/**
	 * To set the paging size for the list
	 * 
	 * @param event
	 * @throws Exception
	 */
	public void onBlur$txb_page_size(Event event) throws Exception {
		setPageSize();
	}

	private void setMovementListModalToListBox() {
		lstbx_movements.setModel(lst_model_movement);
		lstbx_movements.setItemRenderer(new MovementListModelItemRender());
		lstbx_movements.setMold("paging");

		btn_add_movement.setDisabled(false);
		btn_movement_help.setDisabled(false);
		btn_movement_refresh.setDisabled(false);
		btn_search_movement_list.setDisabled(false);

		txb_page_size.setDisabled(false);
		txb_page_size.setValue(null);
		txb_page_size.setMaxlength(2);
	}

	private void setPageSize() {
		if (StringUtils.isNotBlank(txb_page_size.getValue())) {
			try {
				lstbx_movements.setPageSize(Integer.valueOf(txb_page_size
						.getValue()));
			} catch (Exception e) {
				SvmsMessageUtils
						.showErrorMessage("Page size can only be a number");
			}
		} else {
			lstbx_movements.setPageSize(15);
		}
	}

	/**
	 * To make a add vehicle call
	 * 
	 * @param event
	 * @throws Exception
	 */
	public void onClick$btn_add_movement(Event event) throws Exception {
		showDetailView(null);
	}

	/**
	 * Opens the detail view of the Vehicle Controller. <br>
	 * Overhanded some params in a map if needed. <br>
	 * 
	 * @param branche
	 * @throws Exception
	 */
	private void showDetailView(MovementVO movement) throws Exception {

		final HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("selectedMovement", movement);
		map.put("movementListCtrl", this);
		Window win = null;
		try {
			win = (Window) Executions.createComponents(
					SvmsResources.MANAGE_MOVEMENT_DIALOGUE, null, map);
		} catch (final Exception e) {
			e.printStackTrace();
			SvmsMessageUtils.showErrorMessage(e.getMessage());
			if (win != null) {
				win.detach();
			}
		}
	}

	/**
	 * To reset the vehicle list to current values in database
	 * 
	 * @param event
	 * @throws Exception
	 */
	public void onClick$btn_movement_refresh(Event event) throws Exception {
		loadMovementToListModal();
		setMovementListModalToListBox();
	}

	/**
	 * To display help menu
	 * 
	 * @param event
	 * @throws Exception
	 */
	public void onClick$btn_movement_help(Event event) throws Exception {
		StringBuffer helpMessage = new StringBuffer();
		helpMessage
				.append("---------------------------------------------------------");
		helpMessage.append("\n");
		helpMessage.append("1. Add Spare button to add a new vehicle");
		helpMessage.append("\n");
		helpMessage.append("2. Search to find a spare in system");
		helpMessage.append("\n");
		helpMessage.append("3. Refresh to get a latest update from system");
		helpMessage.append("\n");
		helpMessage.append("4. Double click on the vehicle in list to edit it");
		helpMessage.append("\n");
		helpMessage
				.append("---------------------------------------------------------");
		SvmsMessageUtils.showGenericMessage(helpMessage.toString());
	}

	/**
	 * To search for vehicles from the main vehicle window
	 * 
	 * @param event
	 * @throws Exception
	 */
	public void onClick$btn_search_movement_list(Event event) throws Exception {
		Map<String, Object> args = new HashMap<String, Object>();
		args.put("movementListCtrl", this);
		Window win = null;
		try {
			win = (Window) Executions.createComponents(
					SvmsResources.MOVEMENT_SEARCH_DIALOGUE, null, args);
		} catch (final Exception e) {
			e.printStackTrace();
			SvmsMessageUtils.showErrorMessage(e.getMessage());
			if (win != null) {
				win.detach();
			}
		}
	}

	/**
	 * Forwarded from the {@link MovementListModelItemRender} to perform the
	 * double click event on the list items
	 * 
	 * @param event
	 * @throws Exception
	 */
	public void onMovementItemDoubleClicked(Event event) throws Exception {

		Listitem item = this.lstbx_movements.getSelectedItem();

		if (item != null) {
			MovementVO movement = (MovementVO) item
					.getAttribute("selectedMovement");
			System.out.println("...............");
			System.out.println(movement);
			System.out.println("...............");
			showDetailView(movement);
		}

	}
}
